package com.app.toknow.Common;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kangw on 2015/10/3.
 */
public class ParseJsonUtil {
    private static final String TAG = "ParseJsonUtil";
    public enum JsonDataType{
        Simple,
        Object,
        List
    }
    /*
    * 解析 JsonString
    * result - json数据
    * */
    public static Map<String, Object> parseJSONString(String result,JsonDataType dataType) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            // 直接把JSON字符串转化为一个JSONObject对象
            JSONObject jsonOBJ = new JSONObject(result);
            Map<String, Object> map = new HashMap<String, Object>();
            String[] fields = new String[]{"STATE","MESSAGE","CLASS"};
            int state = jsonOBJ.getInt("STATE");
            String message = jsonOBJ.getString("MESSAGE");
            String className = jsonOBJ.getString("CLASS");
            resultMap.put("STATE",state);
            resultMap.put("MESSAGE",message);
            resultMap.put("CLASS",className);
            if(state == 1)
            {
                JSONArray jsonArray = null;
                switch (dataType){
                    case Simple:
                        break;
                    case Object:
                        jsonArray = jsonOBJ.getJSONArray("Entity");
                        resultMap.put("Entity",GetDataToObject((JSONObject)jsonArray.opt(0),className));
                        break;
                    case List:
                        jsonArray = jsonOBJ.getJSONArray("List");
                        resultMap.put("List",GetDataToArrayList(jsonArray, className));
                        break;
                    default:
                        break;
                }

            }
//            for (String str : fields) {
//                resultMap.put(str, jsonOBJ.get(str));
//                if(str == "STATE"){
//                    state = jsonOBJ.getInt("STATE");
//                }
//                if(str == "CLASS")
//
//                resultMap.put("UserID", jsonOBJ.getInt("STATE") == 1?jsonOBJ.getInt("UserID"):-1);
//
//            }1
//            // 第1个键值对
//            resultMap.put("STATE",jsonOBJ.getInt("STATE"));
//            // 第2个键值对
//            resultMap.put("MESSAGE", jsonOBJ.getString("MESSAGE"));
//            // 第3个键值对
//            resultMap.put("UserID", jsonOBJ.getInt("UserID"));
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage());
        }
        return resultMap;
    }
    private static ArrayList<Object> GetDataToArrayList(JSONArray jsonArray,String className)
    {
        ArrayList<Object> arrayList = null;
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObjectSon = (JSONObject) jsonArray.opt(i);
            arrayList.add(GetDataToObject(jsonObjectSon,className));
        }
        return null;
    }
    private static Object GetDataToObject(JSONObject jsonObjectSon,String className) {
        Class<?> objectClass = null;
        Object entity = null;
        try {
            objectClass = Class.forName("com.example.kangw.myapplication.Entity.DBTable." + className);
            // 取得本类的全部属性
            Field[] field = objectClass.getDeclaredFields();
            String propertyName = "";
            entity = objectClass.newInstance();
            for (int column = 0; column < field.length; column++) {
                // 属性类型
                Object type = field[column].getType();
                propertyName = field[column].getName();
                String[] types =  type.toString().split(".");
                if(type.toString().indexOf("String") > 0|| type.toString().indexOf("Time") > 0)
                {
                    Method method = objectClass.getMethod("set" + propertyName, String.class);
                    method.invoke(entity, jsonObjectSon.getString(propertyName));
                }
                else if(type.toString().indexOf("int") > -1)
                {
                    Method method = objectClass.getMethod("set" + propertyName, int.class);
                    method.invoke(entity, jsonObjectSon.getInt(propertyName));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
    public static<T> T SetBundelValue(String className, Map<String, Object> list,String name)
    {
        Class<?> objectClass = null;
//        try {
//            objectClass = Class.forName("com.example.kangw.myapplication.Entity.DBTable." + className);
//            objectClass.cl classN = (objectClass.getClass())list.get(name);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

        return null;
    }
}
